/**
 * 
 */
package com.gwcloud.leetcode.hot100;

import com.gwcloud.leetcode.common.datastructure.ListNode;

/**
 * @ClassName   : Leetcode206 
 * @author      : ganwei 
 * @date        : 2020年4月30日
 * @Description : Leetcode206 反转链表
 */
public class Leetcode0206 {
	/**
	 * -解题思路，递归
	 */
    public static ListNode reverseList(ListNode head) {
    	if(head == null || head.next == null){
    		return head;
    	}
    	ListNode p = reverseList(head.next);
    	head.next.next = head;
    	head.next = null;
    	return p;
    	
    }
    /**
     * -解题思路，迭代，链表反转
     */
    public static ListNode reverseList_1(ListNode head){

    	if(head == null || head.next == null){
    		return head;
    	}
    	
    	ListNode indexa = head;
    	ListNode indexb = head.next;
    	while(indexb != null){
    		ListNode indext = indexb.next;
    		indexb.next = indexa;
    		if(indexa == head){
    			indexa.next = null;
    		}
    		
    		indexa = indexb;
    		indexb = indext;
    		
    	}
    	return indexa;
    }
    public static void main(String args[]){
    	ListNode n1 = new ListNode(1);
    	ListNode n2 = new ListNode(2);
    	ListNode n3 = new ListNode(3);
    	ListNode n4 = new ListNode(4);
    	ListNode n5 = new ListNode(5);
    	n1.next = n2;
    	n2.next = n3;
    	n3.next = n4;
    	n4.next = n5;
    	n5.next = null;
    	ListNode tail = reverseList_1(n1);
    	while(tail != null){
    		System.out.print(tail.val);
    		tail = tail.next;
    	}
    }
}
